﻿(function (w) {
    function getAuthCode(content,url,pageType){
        this.content=content;
        this.url=url;
        this.pageType=pageType;
        this.componentFlag=true;
    }
    getAuthCode.prototype={
        init: function () {
            var that=this;
            this.setMHtml(this.content);
            $(".send-message").on("click", function (e) {
                if($(this).hasClass("disabled")){
                    return;
                }
                that.validCodeShow(that.url, that.pageType);
                if($(".form-vaid-code").attr("state").toString()=="true"){
                    if($(".form-vaid-code input").val()==""||$(".form-vaid-code input").val()==null){
                        showTip("请输入验证码");
                    }
                }
                e.stopPropagation();
            });
            $(".code-img").on("click", function (e) {
                that.imageAuthCodeRefresh("outputImage");
                e.stopPropagation();
            })
        },
        setMHtml: function (obj) {
            obj.append(
                '<div id="sms-code">'+
                '<div class="list form-phone">' +
                '<input type="text" id="mobile-ipt" placeholder="您的手机" maxlength="11">' +
                '</div>' +
                '<div class="list form-vaid-code" state="false">' +
                '<input type="text" maxlength="4" placeholder="验证码" id="vaid-code-ipt">' +
                '<div class="code-img">' +
                '<img src="" id="image-auth-code">' +
                '<input id="image-auth-code-id-input" name="page1ImageAuthCodeId" value="" type="hidden" />' +
                '</div>' +
                '</div>' +
                '<div class="list form-message-code">' +
                '<input type="text" id="vaid-code"  maxlength="6" placeholder="动态码">' +
                '<a class="send-message">获取动态码</a>' +
                '</div>'+
                '</div>'
            )
        },
        validCodeShow:function(url, pageType){
            var that=this;
            execute();

            function execute(){
                var phoneNum=$(".form-phone input").val();
                var imgVal=$("#vaid-code-ipt").val();
                //手机号必须输入正确
                if(/^1[34578]\d{9}$/.test(phoneNum)){
                    if($(".form-vaid-code").attr("state").toString()=="true"){
                        //验证码不为空，发送数据，判断验证码是否正确，否则提示验证码错误。
                        if(imgVal!="" && imgVal!=null){
                            $.ajax({
                                url:url,
                                dateType:"jsonp",
                                async:false,
                                data:{
                                    "mobile" : phoneNum,
                                    "imageAuthCode" : imgVal,
                                    "imageAuthCodeId" : $("#image-auth-code-id-input").val(),
                                    "pageType" : pageType
                                },
                                success: function (data) {
                                    console.log("33");
                                    console.log(data);
                                    //判断验证码是否正确
                                    if(data.code=="-1"){
                                        //validValidCode("图片验证码错误");
                                        showTip("图片验证码错误");
                                        $(".code-img").click();
                                    }
                                    else if(data.code=="201003"){
                                        //validValidCode("图片验证码错误");
                                        showTip("图片验证码错误");
                                        $(".code-img").click();
                                    }
                                    else if(data.code=="200"){
                                        that.componentsNewNew($(".send-message"));
                                    }else{
                                        showTip("图片验证码错误");
                                        $(".code-img").click();
                                    }
                                }
                            })
                        }else{
                            showTip("请输入验证码");
                        }
                    }else{
                        $.ajax({
                            url:url,
                            dateType:"jsonp",
                            async:false,
                            data:{
                                "mobile" : $(".form-phone input").val(),
                                "pageType" : pageType
                            },
                            success: function (data) {
                                console.log(data.code);
                                if(data.code=="201003"){
                                    $(".form-vaid-code").css("display","block");
                                    $(".form-vaid-code").attr("state","true");
                                    $(".code-img").click();
                                }else if(data.code=="200"){
                                    that.componentsNewNew($(".send-message"));
                                }else if(data.code=="201002"){
                                    //showError($('input[name="principal.mobile"]'), data.message);
                                    showTip(data.message);
                                }else if(data.code=="100001"){
                                    //showError($('input[name="principal.mobile"]'), data.message);
                                    showTip(data.message);
                                }else{
                                    //showError($('input[name="principal.mobile"]'), "未知错误");
                                    showTip("未知错误");
                                }
                            },
                        })
                    }
                }else{
                    showTip("手机号码错误");
                }
            }


        },
        componentsNewNew: function (obj) {
            var time = 60;
            if (this.componentFlag) {
                obj.addClass("disabled");
                obj.html('重发&nbsp;&nbsp;(60s)').css({
                    'border-color': '#ccc',
                    'color': '#ccc'
                });
                var t = setInterval(count, 1000);
                this.componentFlag = false;
            }
            function count() {
                time -= 1;
                obj.html('重发&nbsp;&nbsp;(' + time + 's)').css({
                    'border-color': '#ccc',
                    'color': '#ccc'
                });
                if (time <= 0) {
                    clearInterval(t);
                    obj.html('获取动态码').css({
                        'border-color': '#ff5252',
                        'color': '#ff5252'
                    });
                    this.componentFlag = true;
                    obj.removeClass("disabled");
                }
            }
        },
        imageAuthCodeRefresh: function () {
            $.ajax({
                type: 'GET',
                url: Mall.home + '/authCode/getImageAuthCode',
                success: function(data) {
                    console.log(data);
                    codeImg = data.object.imgBase64Src;
                    authCodeId = data.object.authCodeId;
                    $('#image-auth-code').attr('src', codeImg);
                    $("#image-auth-code-id-input").val(authCodeId);
                },
                error: function(type, xhr) {
                    console.log(type);
                    console.log(xhr);
                }
            })
        }
    }
    w.getAuthCode=getAuthCode;
}(window))


